import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart' as sc;
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:ysy_flutter_mvvm_yuanxuan/base/LoadDialog.dart';

import 'package:ysy_flutter_mvvm_yuanxuan/helper/SmartRefresherHelper.dart';
import 'package:ysy_flutter_mvvm_yuanxuan/view/AppMain.dart';
import 'package:ysy_flutter_mvvm_yuanxuan/view/cartshoppingwidget/CartListWidget.dart';
import 'package:ysy_flutter_mvvm_yuanxuan/view/goods_details/GoodsDetails.dart';
import 'package:ysy_flutter_mvvm_yuanxuan/view/launch/Launch.dart';
import 'package:ysy_flutter_mvvm_yuanxuan/view/login/LoginWidget.dart';
import 'package:ysy_flutter_mvvm_yuanxuan/view/order/OrderWidget.dart';
import 'package:ysy_flutter_mvvm_yuanxuan/view/subit_order/SubitOrderWidget.dart';
import 'package:ysy_flutter_mvvm_yuanxuan/widget/OverScrollBehavior.dart';

import 'AppViewModel.dart';
import 'base/PageProvideNode.dart';


//  请注意  README.md

void main() {
  runApp( MyApp());
}
RouteObserver<PageRoute> routeObserver = RouteObserver<PageRoute>();
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return sc.ScreenUtilInit(
        designSize: Size(380, 720),
      builder: (content,widget)=> MRefreshConfiguration(MaterialApp(
        localizationsDelegates: [
          // 这行是关键
          RefreshLocalizations.delegate,
        ],
        navigatorObservers: [routeObserver],
        routes: initRoutes(),
        scrollBehavior: OverScrollBehavior(),
        title: '仿源选',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: MyPageProvideNode(),
      )),
    );
  }

  //初始化路由
  static initRoutes() {
    Map<String, WidgetBuilder> routes = {
      'login': (context) => LoginWidget(),
      'goodsdetails': (context) => GoodsDetails(),
      'OrderWidget': (context) => OrderWidget(),
      'CartListWidget': (context) => CartListWidget(),
      'SubitOrderWidget': (context) => SubitOrderWidget(),
      'AppMain': (context) => AppMain(),
    };
    return routes;
  }

}

class MyPageProvideNode extends PageProvideNode<TestViewModel> {
  MyPageProvideNode() : super(TestViewModel());

  @override
  Widget buildContent(BuildContext context) {
    return MyHomePage(mProvider,title: 'Flutter Demo Home Page');
  }
}

class MyHomePage extends StatefulWidget {

  TestViewModel testViewModel;

  MyHomePage(this.testViewModel,{Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
      // sc.ScreenUtil.init(
      //     BoxConstraints(
      //         maxWidth: MediaQuery.of(context).size.width,
      //         maxHeight: MediaQuery.of(context).size.height),
      //     designSize: Size(380, 720),
      //     orientation: Orientation.portrait);
      SpUtil.getInstance();
      Navigator.of(context).push(MaterialPageRoute(builder: (context){
        return LaunchApp();
      }));
    });
  }

  @override
  Widget build(BuildContext context) {
    LoadDialog.ctx=context;
    return Scaffold(body: Text(""));
  }


}
